

libname indiv '...';
libname indiv2 '...';


%let pce1979=37.178;
%let pce1980=41.182;
%let pce1981=44.871;
%let pce1982=47.363;
%let pce1983=49.378;
%let pce1984=51.243;
%let pce1985=53.031;
%let pce1986=54.184;
%let pce1987=55.855;
%let pce1988=58.038;
%let pce1989=60.572;
%let pce1990=63.231;
%let pce1991=65.346;
%let pce1992=67.087;
%let pce1993=68.758;
%let pce1994=70.193;
%let pce1995=71.671;
%let pce1996=73.204;
%let pce1997=74.478;
%let pce1998=75.07;
%let pce1999=76.164;
%let pce2000=78.091;
%let pce2001=79.656;
%let pce2002=80.702;
%let pce2003=82.398;
%let pce2004=84.443;
%let pce2005=86.876;
%let pce2006=89.322;
%let pce2007=91.614;
%let pce2008=94.326;
%let pce2009=94.063;
%let pce2010=95.747;
%let pce2011=98.169;
%let pce2012=100;
%let pce2013=101.354;
%let pce2014=102.887;
%let pce2015=103.116;
%let pce2016=104.148;
%let pce2017=106.054;
%let pce2018=108.317;
%let pce2019=109.933;
%let pce2020=111.145;
%let pce2021=115.621;
%let pce2022=122.861;

%macro EICdistrib(iyr, fyr);

%do yr = &iyr %to &fyr; 

%let pyr = %eval(&yr-1);

%IF (&yr >= 1979) %then %do; 
data TAB; set indiv.soi&yr;  * earlier PUF data has no advance amounts because became available in 1979;
  ct=1;
  marr=0; if mars=2 then marr=1; 

  * 1998, 2000-2013;
  agi    = E00100; 
  AdvEIC = E10000; * Total Adv EIC;
  EIC    = E11000; * total EIC;
  EIC2   = E59660; * Total earned income credit;
  nrfEIC = E59680; * EIC used to offset income tax before credits;
  nrfEIC2 = E59700; * EIC used to offset all other taxes except adv EIC;
  refEIC = E59720; * EIC refundable portion;
  trueup = E59740; * EIC in excess of advance EIC payments;
  repay  = E59760; * Advance EIC payments in excess of EIC;
  wages  = E00200; if wages=. then wages=0;
  seinc  = E00900; if seinc=. then seinc=0;
  earn = wages + seinc;

* Only available since about 2008/9/10 (other than pymars);
  pmars = pymars; * prior year marital status;
  pagi = P00100; * Prior Year AGI;				
  ptax = P04800; * Prior Year Taxable Income;	
  peic = P11000; * Prior Year Earned Income Credit;		
  year = &yr; 

 %if &yr=1999 %then %do;
  agi    =  E27; *E00100; 
  AdvEIC = E163; *E10000; * Total Adv EIC;
  EIC    = E175; *E11000; * total EIC;
  EIC2   = E794; *E59660; * Total earned income credit;
  nrfEIC = E795; *E59680; * EIC used to offset income tax before credits;
  nrfEIC2 = E796; *E59700; * EIC used to offset all other taxes except adv EIC;
  refEIC = E797; *E59720; * EIC refundable portion;
  trueup = E798; *E59740; * EIC in excess of advance EIC payments;
  repay  = E799; *E59760; * Advance EIC payments in excess of EIC;
 %end;

 %if &yr=1997 %then %do;
  agi     =  E26; *E00100; 
  AdvEIC  = E147; *E10000; * Total Adv EIC;
  EIC     = E160; *E11000; * total EIC;
  EIC2    = E684; *E59660; * Total earned income credit;
  nrfEIC  = E685; *E59680; * EIC used to offset income tax before credits;
  nrfEIC2 = E686; *E59700; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E687; *E59720; * EIC refundable portion;
  trueup  = E688; *E59740; * EIC in excess of advance EIC payments;
  repay   = E689; *E59760; * Advance EIC payments in excess of EIC;
 %end;

 %if &yr=1996 %then %do;
  agi     =  E26; *E00100; 
  AdvEIC  = E142; *E10000; * Total Adv EIC;
  EIC     = E154; *E11000; * total EIC;
  EIC2    = E701; *E59660; * Total earned income credit;
  nrfEIC  = E702; *E59680; * EIC used to offset income tax before credits;
  nrfEIC2 = E703; *E59700; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E704; *E59720; * EIC refundable portion;
  trueup  = E705; *E59740; * EIC in excess of advance EIC payments;
  repay   = E706; *E59760; * Advance EIC payments in excess of EIC;
 %end;

 %if &yr=1995 %then %do;
  agi     =  E26; *E00100; 
  AdvEIC  = E138; *E10000; * Total Adv EIC;
  EIC     = E149; *E11000; * total EIC;
  nrfEIC  = E702; *E59680; * EIC used to offset income tax before credits;
  nrfEIC2 = E703; *E59700; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E704; *E59720; * EIC refundable portion;
  trueup  = E705; *E59740; * EIC in excess of advance EIC payments;
  repay   = E706; *E59760; * Advance EIC payments in excess of EIC;
 %end;

 %if &yr=1994 %then %do;
  agi     =  E24; 
  AdvEIC  = E137; * Total Adv EIC;
  EIC     = E147; * total EIC;
  EIC2    = E629; * EIC computed;
  nrfEIC  = E630; * EIC used to offset income tax before credits;
  nrfEIC2 = E631; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E633; * EIC refundable portion;
  trueup  = 0; if (AdvEIC>0)and(EIC2>AdvEIC) then trueup = EIC2-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  trueup2 = E633; * EIC IN EXCESS-ADV EIC PYMNTS;
  repay2  = E634; * Advance EIC payments in excess of EIC; *E623: Earned income;
 %end;

 %if &yr=1993 %then %do;
  agi     =  E24; 
  AdvEIC  = E135; * Total Adv EIC;
  EIC     = E146; * total EIC;
  EIC2    = E630; * EIC computed;
  nrfEIC  = E631; * EIC used to offset income tax before credits;
  nrfEIC2 = E632; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E633; * EIC refundable portion;
  trueup  = 0; if (AdvEIC>0)and(EIC2>AdvEIC) then trueup = EIC2-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  trueup2 = E634; * EIC IN EXCESS-ADV EIC PYMNTS;
  repay2  = E635; * Advance EIC payments in excess of EIC; *E624: Earned income;
 %end;

 %if &yr=1992 %then %do;
  agi     =  E24; 
  AdvEIC  = E131; * Total Adv EIC;
  EIC     = E142; * total EIC;
  EIC2    = E626; * EIC computed;
  nrfEIC  = E627; * EIC used to offset income tax before credits;
  nrfEIC2 = E628; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E629; * EIC refundable portion;
  trueup  = 0; if (AdvEIC>0)and(EIC2>AdvEIC) then trueup = EIC2-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  trueup2 = E630; * EIC IN EXCESS-ADV EIC PYMNTS;
  repay2  = E631; * Advance EIC payments in excess of EIC; *E620: Earned income;
 %end;

 %if &yr=1991 %then %do;
  agi     =  E24; 
  AdvEIC  = E137; * Total Adv EIC;
  EIC     = E148; * total EIC;
  EIC2    = E589; * EIC computed;
  nrfEIC  = E590; * EIC used to offset income tax before credits;
  nrfEIC2 = E591; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E592; * EIC refundable portion;
  trueup  = 0; if (AdvEIC>0)and(EIC2>AdvEIC) then trueup = EIC2-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  trueup2 = E593; * EIC IN EXCESS-ADV EIC PYMNTS;
  repay2  = E594; * Advance EIC payments in excess of EIC; *E583: Earned income;
 %end;

 %if &yr=1990 %then %do;
  agi     =  E24; 
  AdvEIC  = E147; * Total Adv EIC;
  EIC     = E158; * total EIC;
  EIC2    = E579; * EIC computed;
  nrfEIC  = E582; * EIC used to offset income tax before credits;
  nrfEIC2 = E583; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E584; * EIC refundable portion;
  trueup  = 0; if (AdvEIC>0)and(EIC2>AdvEIC) then trueup = EIC2-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  trueup2 = E585; * EIC IN EXCESS-ADV EIC PYMNTS;
  repay2  = E586; * Advance EIC payments in excess of EIC; *E581: Earned income;
 %end;

 %if &yr=1989 %then %do;
  agi     =  E22; 
  AdvEIC  = E125; * Total Adv EIC;
  EIC     = E135; * total EIC;
  EIC2    = E183; * EIC computed;
  nrfEIC  = E186; * EIC used to offset income tax before credits;
  nrfEIC2 = E187; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E188; * EIC refundable portion;
  trueup  = 0; if (AdvEIC>0)and(EIC2>AdvEIC) then trueup = EIC2-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  repay2  = E190; * Advance EIC payments in excess of EIC; *E185: Earned income;
 %end;

 %if &yr=1988 %then %do;
  agi     =  E22; 
  AdvEIC  = E128; * Total Adv EIC;
  EIC     = E139; * total EIC;
  EIC2    = E183; * EIC computed;
  nrfEIC  = E186; * EIC used to offset income tax before credits;
  nrfEIC2 = E187; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E188; * EIC refundable portion;
  trueup  = 0; if (AdvEIC>0)and(EIC2>AdvEIC) then trueup = EIC2-AdvEIC; * EIC in excess of advance EIC payments;
  *truep   = E189; * same result;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  *repay  = E190; *Advance EIC payments in excess of EIC same result;
  *E185: Earned income E178: Wholly refundable EIC;
 %end;

 %if &yr=1987 %then %do;
  agi     =  E22; 
  AdvEIC  = E128; * Total Adv EIC;
  EIC     = E139; * total EIC;
  EIC2    = E183; * EIC computed;
  nrfEIC  = E186; * EIC used to offset income tax before credits;
  nrfEIC2 = E187; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E188; * EIC refundable portion;
  trueup  = 0; if (AdvEIC>0)and(EIC2>AdvEIC) then trueup = EIC2-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  repay2  = E190; *Advance EIC payments in excess of EIC;
  *E185: Earned income E178: Wholly refundable EIC;
 %end;

 %if &yr=1986 %then %do;
  agi     =  E22; 
  AdvEIC  = E131; * Total Adv EIC;
  EIC     = E142; * total EIC;
  EIC2    = E176; * EIC computed;
  nrfEIC  = E179; * EIC used to offset income tax before credits;
  nrfEIC2 = E180; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E181; * EIC refundable portion;
  trueup  = 0; if (EIC>AdvEIC) then trueup = EIC-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  repay2  = E183; *Advance EIC payments in excess of EIC;
  *E178: Earned income E178: Wholly refundable EIC;
 %end;

 %if &yr=1984 or &yr=1985 %then %do;
  agi     =  E22; 
  AdvEIC  = E121; * Total Adv EIC;
  EIC     = E131; * total EIC;
  EIC2    = E176; * EIC computed;
  nrfEIC  = E179; * EIC used to offset income tax before credits;
  nrfEIC2 = E180; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E181; * EIC refundable portion;
  trueup  = 0; if (EIC>AdvEIC) then trueup = EIC-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  repay2  = E183; *Advance EIC payments in excess of EIC;
  *E178: Earned income E178: Wholly refundable EIC;
 %end;

 %if &yr=1982 or &yr=1983 %then %do;
  agi     =  E10; 
  AdvEIC  = E1000; * Total Adv EIC;
  EIC     = E1100; * total EIC;
  EIC2    = E1550; * EIC computed;
  nrfEIC  = E1580; * EIC used to offset income tax before credits;
  nrfEIC2 = E1590; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E1600; * EIC refundable portion;
  trueup  = 0; if (EIC>AdvEIC) then trueup = EIC-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
  repay2  = E1620; *Advance EIC payments in excess of EIC;
  *E1570: Earned income E1640: Wholly refundable EIC;
 %end;

 %if &yr=1981 %then %do;
  agi     =  E76; 
  AdvEIC  = E113; * Total Adv EIC;
  EIC     = E117; * total EIC;
  nrfEIC  = E788; * EIC used to offset income tax before credits;
  nrfEIC2 = E789; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E790; * EIC refundable portion;
  trueup  = 0; if (EIC>AdvEIC) then trueup = EIC-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
 %end;

 %if &yr=1980 %then %do;
  agi     =  E73; 
  AdvEIC  = E109; * Total Adv EIC;
  EIC     = E114; * total EIC;
  nrfEIC  = E769; * EIC used to offset income tax before credits;
  nrfEIC2 = E770; * EIC used to offset all other taxes except adv EIC;
  refEIC  = E771; * EIC refundable portion;
  trueup  = 0; if (EIC>AdvEIC) then trueup = EIC-AdvEIC; * EIC in excess of advance EIC payments;
  repay   = 0; if (EIC<AdvEIC) then repay  = AdvEIC-EIC; * Advance EIC payments in excess of EIC;
 %end;

 %if &yr=1979 %then %do;
  agi     =  E73; 
  AdvEIC  = E109; * Total Adv EIC;
  EIC     = E114; * total EIC;
 %end;

 if (EIC2=.) then EIC2=0;
 if (AdvEIC<10) then do; AdvEIC=0; repay=0; trueup=0; end; * remove outliers with adv EITC less than $10;
 if (AdvEIC=0) then trueup=0; * only among those with advance credits;
 pceyr = &&pce&yr;
run;
%end;

proc means data=TAB; var ct; weight xwgt; output out=AdvEICstats
  mean (year)=year 
  sum(AdvEIC EIC EIC2 nrfEIC nrfEIC2 refEIC repay trueup)=
      AdvEIC EIC EIC2 nrfEIC nrfEIC2 refEIC repay trueup; run;

proc append base=AdvEICstatsAll data=AdvEICstats force; run;


* Distributions by income intervals;
data TAB; set TAB;
 * Real values;
 agir   =agi   *&pce2022/&&pce&yr; 
 pagir  =pagi  *&pce2022/&&pce&yr; 
 EICr   =EIC   *&pce2022/&&pce&yr; 
 AdvEICr=AdvEIC*&pce2022/&&pce&yr; 
 repayr =repay *&pce2022/&&pce&yr; 
 trueupr=trueup*&pce2022/&&pce&yr;

 * Current-year AGI groups;
 gp=999;
 %do inc = 1 %to 11; 
	 if agir>((&inc-1)*6000) and agir<= &inc*6000 then gp = &inc; * PCE indexed;
 %end;

 * Negative to zero group;
 if agi<0 then gp=0; 

 * Counts;
 cEIC=0; cAdvEIC=0; cReP=0; cTU=0; 
 if (EIC>0)    then cEIC=1; 
 if (advEIC>0)or(repay>0) then cAdvEIC=1; 
 if (repay>0)  then cRep=1; 
 if (trueup>0) then cTU=1; 

 eickids=0; 
 if (S053 ne .)and(S053 ne 0) then eickids=eickids+1;
 if (S054 ne .)and(S054 ne 0) then eickids=eickids+1;
 if (S055 ne .)and(S055 ne 0) then eickids=eickids+1;
 kids=99;
 if (eickids=1) then kids=1;
 if (eickids>1) then kids=2;

 pagi=.; peickids=.; ppmars=.;

 keep S002 flpdyr year mars pmars earn eickids ct gp xwgt agi EIC AdvEIC repay trueup cEIC cAdvEIC cRep cTU agir pagir EICr AdvEICr repayr trueupr pagi peickids ppmars; 
run;

* MERGE TO PRIOR YEAR;
data pTAB; set indiv.soi&pyr; * prior-year file (some issues from late filers), 1998, 2000-2013;
  if &pyr=1999 then S002 = A8; 
  ppmars=mars;   
  pagi = E00100; if &pyr=1999 then agi = E27; 
  peickids=0; 
  if (S053 ne .)and(S053 ne 0) then peickids=peickids+1; if &pyr=1999 and (E1609 ne .)and(E1609 ne 0) then peickids=peickids+1; 
  if (S054 ne .)and(S054 ne 0) then peickids=peickids+1; if &pyr=1999 and (E1610 ne .)and(E1610 ne 0) then peickids=peickids+1; 
  if (S055 ne .)and(S055 ne 0) then peickids=peickids+1;
  keep S002 ppmars pagi peickids earn;
run;

* merge prior-year file to current-year file;
proc sort  data=TAB;  by S002; run; 
proc sort  data=pTAB; by S002; run; 
data TAB2; merge TAB (in=a) pTAB (in=b); by S002; if a and b; run; * only include if had curent and prior year;

data TAB2; set TAB2;
 * Decomposition;
 RepStatCh=0; RepChDecr=0; RepIncIncr=0; RepEarnDec=0; RepIncInc2 = 0; RepStatDiv=0; RepStatNF=0; RepIncDecNoLimit=0;
 PhsInMx0 = 5220; PhsInMx1 = 7830; PhsInMx2 = 11000;* 2005 phase in max amounts;
 if ppmars ne mars and eickids >= peickids then RepStatCh  = repay; * Repayment: status change only change in marriage (otherwise gets single to HOH) and (ppmars=2 or mars=2);
 if eickids <  peickids                    then RepChDecr  = repay; * Repayment: child decrease(stacked);
 if eickids >= peickids and agi >  pagi    then RepIncIncr = repay; * Repayment: income increase (stacked);
 if eickids >= peickids and agi <  pagi and eickids=0  and earn<(PhsInMx0*&&pce&yr/&pce2005) then RepEarnDec = repay; * Repayment: income decrease below max phase-in amount (stacked);
 if eickids >= peickids and agi <  pagi and eickids=1  and earn<(PhsInMx1*&&pce&yr/&pce2005) then RepEarnDec = repay; * Repayment: income decrease below max phase-in amount (stacked);
 if eickids >= peickids and agi <  pagi and eickids=>2 and earn<(PhsInMx2*&&pce&yr/&pce2005) then RepEarnDec = repay; * Repayment: income decrease below max phase-in amount (stacked);

 if eickids >= peickids and agi <  pagi    then RepIncDecNoLimit  = repay; * Repayment: income decrease (stacked) no limit;

 if ppmars ne mars and ppmars=2               then RepStatDiv = repay; * Repayment: status change (isolate divorces);
 if ppmars ne mars and (ppmars=0 or ppmars=.) then RepStatNF  = repay; * Repayment: status change (isolate new filers);
 if agi >  pagi                               then RepIncInc2 = repay; * Repayment: income increase (not stacked);

 keep ct gp xwgt year agir EICr AdvEICr repayr trueupr cEIC cAdvEIC cRep cTU ct AdvEICr repayr RepStatCh RepChDecr RepIncIncr RepStatNF RepStatDiv RepIncInc2 RepEarnDec;
run;

* Append this year to EICmulti;
proc append base=EICmulti data=TAB2 force; run; 

%end;


* Multi-year distributions by current-year income (real $2022 groups): Figure 6C;
proc sort  data=EICmulti;         by gp; run; 
proc means data=EICmulti; var ct; by gp; weight xwgt; output out=AdvEICdistMulti
  min(year)=firstyr max(year)=lastyr  min(agir)=agimin max(agir)=agimax 
  sum(agir EICr AdvEICr repayr trueupr cEIC cAdvEIC cRep cTU ct)=
      agir EICr AdvEICr repayr trueupr cEIC cAdvEIC cRep cTU ct; run;

* Decomposition: Figure 6D;
data EICmultiDecomp; set EICmulti;	   
 if (repay=0)and(AdvEIC=0) then delete; * only for decomp;
run;

proc means data=EICmultiDecomp; var ct; weight xwgt; output out=AdvEICDecomp
  min(year)=firstyr max(year)=lastyr 
  sum(AdvEICr repayr RepStatCh RepChDecr RepIncIncr RepStatNF RepStatDiv RepIncInc2 RepEarnDec)=
      AdvEICr repayr RepStatCh RepChDecr RepIncIncr RepStatNF RepStatDiv RepIncInc2 RepEarnDec; run;

%mend EICdistrib;

%macro CTCdist2020;

%let yr=2020;
data TAB; set indiv.soi2020;
  ct=1;
  marr=0; if mars=2 then marr=1; 
  agi = e00100; 
  ctc_old =  (e07225 + e11070 + e07220)/1000; * e07220 captures amounts from late returns;
  eitc = e11000/1000;
  nkids  = n24; * N24: Number of Children for Child Tax Credit, Form 1040; 
  all_dep=0; itin_dep=0; 
  %do ct = 25 %to 34; 
    if (S0&ct > 0) then all_dep = all_dep + 1; 
    if (S0&ct > 900000000) then itin_dep = itin_dep + 1; 
  %end;  *all_dep = n6 does not work because no more exemptions;
  *if all_dep>3 then all_dep=3; 

  NonRefCTC = E07225; *1040 line 19, includes other dep credit;
  refCTC    = E11070; * 1040 line 28, same as E82940 Form 8812: refundable child tax credit ;

  OthDep1 = (NonRefCTC+refCTC) - 2000*nkids; if OthDep1<0 then OthDep1=0; *  other dependents credit estimate;
  OthDep2 = 500*itin_dep;        if OthDep2<0 then OthDep2=0; if OthDep2>NonRefCTC then OthDep2=NonRefCTC; *  other dependents credit estimate;
  OthDep3 = 500*(all_dep-nkids); if OthDep3<0 then OthDep3=0; if OthDep3>NonRefCTC then OthDep3=NonRefCTC; *  other dependents credit estimate;

  CTCbfreflimit = E82925; * CTC with other dep credit after phase out before refundability limits;
run;

* Figure 5A;
proc means data=TAB; var ct; weight xwgt; output out=CTCs2020
  sum(nkids all_dep itin_dep NonRefCTC refCTC OthDep1 OthDep2 OthDep3)=
      nkids all_dep itin_dep NonRefCTC refCTC OthDep1 OthDep2 OthDep3; run;

%mend CTCdist2020;

%macro CTCdist2021;

%let yr=2021;
data TAB; set indiv.soi2021;
  ct=1; year=&yr;
  marr=0; if mars=2 then marr=1; 
  agi = e00100; 
  nkids  = n24; * Number of Children for Child Tax Credit, Form 1040;

  * 2021 CTC variables;
  NonRefCTC = E07225; * Form 1040 Nonrefundable CTC and credit for other dependents;
  tuCTC  = E11070; * Form 1040 Refundable CTC, includes refundable E82982 and additional E82982 amounts;
  CTC = NonRefCTC + tuCTC; * CTC claimed on a return, excludes advance amounts;
  aCTC   = E82976; * Form 8812: aggregate amount of advance CTC payments self and spouse;
  refCTCbf = E82982; * FOrm 8812: refundable child tax credit before limitations;
  excess = E83002; * Form 8812 Part III: excess payments;
  repay = E83018; * Form 8812 additional tax goes on sch 2 tax line 19 not an offset to CTC lines on 1040;
  repaySch2 = E09985; * Repayment Alt: 1040 Sch 2 other taxes: Additional tax from Schedule 8812;
  SHeff  = excess - repay;
  SHeff2  = excess - repaySch2;
  if repay>aCTC then aCTC=repay; * some missing adv CTC amounts;

  * Only available since about 2008/9/10;
  pmars = PRIORMARS; * prior year marital status pymars;
  pagi = P00100; * Prior Year AGI;					

 * Current-year AGI groups;
 gp=999;
 %do inc = 1 %to 15; 
     if agi>=((&inc-1)*40000) and agi< &inc*40000 then gp = &inc;
 %end; if agi<0 then gp = -1;

 * Prior-year AGI groups;
 pgp=999;
 %do inc = 1 %to 15; 
     if pagi>((&inc-1)*25000) and pagi<= &inc*25000 then pgp = &inc;
 %end;

  cCTC=0; cAdvCTC=0; cRepay=0; cSH=0; 
  if NonRefCTC>0 or tuCTC>0 then cCTC=1;
  if aCTC>0  then cAdvCTC=1;
  if repay>0 then cRepay=1;
  if SHeff>0 then cSH=1;

* Decomposition;
SHincincr=0; SHchdecr=0;
if agi >  pagi then SHincincr = SHeff; * Safe Harbor: income increase;
if agi <= pagi then SHchdecr  = SHeff; * Safe Harbor: child decrease (stacked), should be no other reasons among current-year filers;

RepStatCh=0; RepIncIncr=0; RepChDecr=0; RepIncInc2 = 0; RepStatDiv=0; RepStatNF=0;
if pmars ne mars                then RepStatCh  = repay; * Repayment: status change;
if pmars ne mars and pmars=2    then RepStatDiv = repay; * Repayment: status change (isolate divorces);
if pmars ne mars and pmars=0    then RepStatNF  = repay; * Repayment: status change (isolate new filers);
if pmars = mars and agi >  pagi then RepIncIncr = repay; * Repayment: income increase (stacked);
if                  agi >  pagi then RepIncInc2 = repay; * Repayment: income increase (not stacked);
if pmars = mars and agi <= pagi then RepChDecr  = repay; * Repayment: child decrease (and other, but should only be decreases in number of qualifying children) (stacked);

  keep ct xwgt year gp agi refCTCbf tuCTC CTC repay SHeff NonRefCTC SHincincr SHchdecr RepStatCh RepIncIncr RepChDecr aCTC cCTC cAdvCTC cRepay cSH;
run;

* Figure 5A/C/D;
proc means data=TAB; var ct; weight xwgt; output out=AdvCTCstats2021
  sum( tuCTC CTC repay SHeff NonRefCTC SHincincr SHchdecr RepStatCh RepIncIncr RepChDecr)=
       tuCTC CTC repay SHeff NonRefCTC SHincincr SHchdecr RepStatCh RepIncIncr RepChDecr; run;

* Figure 5B: Distribution, CTC counts and amounts by AGI groups;
proc sort  data=TAB;         by gp; run; 
proc means data=TAB; var ct; by gp; weight xwgt; output out=CTCdist
           mean (year)=year min(agi)=agimin max(agi)=agimax 
           sum(agi CTC aCTC repay SHeff cCTC cAdvCTC cRepay cSH ct)=
               agi CTC aCTC repay SHeff cCTC cAdvCTC cRepay cSH ct; run;

%mend CTCdist2021;

%macro TransitionTables(yr);

data TAB; set indiv.soi&yr;
  ct=1; year=&yr;
  marr=0; if mars=2 then marr=1; 
  agi = e00100; 
  nkids  = n24; * N24: Number of Children for Child Tax Credit, Form 1040;

  * Only available since about 2008/9/10;
  pmars = PRIORMARS; * prior year marital status pymars;
  pagi = P00100; * Prior Year AGI;				

 * 8 gps: Current-year AGI groups;
 gp=0;
 if                  agi<  25000 then gp = 1; *  <$25K;
 if agi>=  25000 and agi<  50000 then gp = 2; * 25-50K;
 if agi>=  50000 and agi<  75000 then gp = 3; 
 if agi>=  75000 and agi< 100000 then gp = 4; 
 if agi>= 100000 and agi< 150000 then gp = 5; 
 if agi>= 150000 and agi< 200000 then gp = 6; 
 if agi>= 200000 and agi< 400000 then gp = 7; 
 if agi>= 400000 and agi< 500000 then gp = 8; 
 if agi>= 500000                 then gp = 9; * >500K;

 * Prior-year AGI groups;
 pgp=0;
 if                   pagi<  25000 then pgp = 1; *  <$25K;
 if pagi>=  25000 and pagi<  50000 then pgp = 2; * 25-50K;
 if pagi>=  50000 and pagi<  75000 then pgp = 3; 
 if pagi>=  75000 and pagi< 100000 then pgp = 4; 
 if pagi>= 100000 and pagi< 150000 then pgp = 5; 
 if pagi>= 150000 and pagi< 200000 then pgp = 6; 
 if pagi>= 200000 and pagi< 400000 then pgp = 7; 
 if pagi>= 400000 and pagi< 500000 then pgp = 8; 
 if pagi>= 500000                  then pgp = 9; * >500K;

 if (pmars=. or pmars=0) then pgp=.; * prior-year nonfilers;
 if (pmars=. or pmars=0) and E02400=0 and E02500=0 and agi>100 then pgp=0 ; * prior-year nonfiler and no SS income in later year;

 %do c = 1 %to 9; 
     gp&c = 0; f&c=0; n&c=0; lk&c=0;
     if gp=&c then gp&c = 1;    
 %end;

 * Figure 1: Many groups to $250K: Current-year AGI groups (fig 1 density);
 gg=0;
 %let incm =  5000; %do inc = 1 %to 50; if agi>=((&inc-1)*&incm) and agi<&inc*&incm then gg = &inc; %end; * 0-5, 5-10,...,245-250; 
 if agi>=250000 then gg = 51; * >250K;
 gun = gg; (if mars=2) or (nkids ne 1) then gun = 9999; * unmarried with 1 CTC kid;

 if dsi=1 then delete; * no dependent filers;

  keep gp pgp ct xwgt year agi gp1-gp9 gg gun;
run;

data  MFJ; set TAB; if mars ne 2 then delete; run;
data nMFJ; set TAB; if mars eq 2 then delete; run;

* Counts by AGI groups;

* MFJ final year;
proc sort  data=MFJ;         by pgp; run; 
proc means data=MFJ; var ct; by pgp; weight xwgt; output out=AGImobMFJ 
               sum(gp1 gp2 gp3 gp4 gp5 gp6 gp7 gp8 gp9)= /autoname; run;
proc means data=MFJ; var ct; by pgp; output out=AGImobMFJobs sum(gp1 gp2 gp3 gp4 gp5 gp6 gp7 gp8 gp9)= /autoname; run;

* Not MFJ final year;
proc sort  data=nMFJ;         by pgp; run; 
proc means data=nMFJ; var ct; by pgp; weight xwgt; output out=AGImobMFJnot 
               sum(gp1 gp2 gp3 gp4 gp5 gp6 gp7 gp8 gp9)= /autoname; run;
proc means data=nMFJ; var ct; by pgp; output out=AGImobMFJnotObs sum(gp1 gp2 gp3 gp4 gp5 gp6 gp7 gp8 gp9)= /autoname; run;

* Figure 1: density of non-dependent umarried tax returns with CTC kids in 2021;
proc sort  data=TAB;         by gun; run; 
proc means data=TAB; var ct; by gun; output out=AGIinitUnmarrKids
               sum(xwgt)=mass max(agi)= mx_agi sum(ct)=ct; run;

* Density of all non-dependent tax returns in 2021 (extra data);
proc sort  data=TAB;         by gg; run; 
proc means data=TAB; var ct; by gg; output out=AGIinit
               sum(xwgt)=mass max(agi)= mx_agi; run;

%mend TransitionTables;

proc datasets library=work; delete AdvEICstatsAll EICmulti; quit; run; 

%EICdistrib(1979,1999); * Figure 6 A/B;
*%EICdistrib(2000,2010); * Figure 6 A/B/C/D;

*%CTCdist2020; * Figure 5A;
*%CTCdist2021; * Figure 5 A/B/C/D;

*%TransitionTables(2021);  * Fig 1 density, Table A1;

